﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static RhythmGame.Settings;

namespace RhythmGame
{
    public partial class ProtectedBreakTap : NoteTap
    {
        public ProtectedBreakTap()
        {
            InitializeComponent();
        }

        internal static void NextTaps(Game game, BeatMap map, List<ProtectedBreakTap> currentProtectedBreakTaps, Element_info[] blockElements)
        {
            foreach (Element_info _element in blockElements)
            {
                if (_element.TypeId == 4)
                {
                    ElementProtectedBreakTap_info element = _element as ElementProtectedBreakTap_info;

                    ProtectedBreakTap protectedBreakTap = new ProtectedBreakTap();

                    // 设置出现时间
                    protectedBreakTap.AppearTime = element.ClickTime - Settings.Instance.DuringTime;

                    protectedBreakTap.JudgeTime = element.ClickTime;

                    // 设置轨道
                    protectedBreakTap.Track = element.X - 1;

                    protectedBreakTap.Location = new Point(0, 0);

                    protectedBreakTap.Width = element.Width * 40;
                    protectedBreakTap.Height = protectedBreakTap.Width / 4;
                    protectedBreakTap.BackColor = Settings.Instance.BreakBlocksColor;

                    // 设置边框颜色和粗细
                    protectedBreakTap.BorderStyle = BorderStyle.FixedSingle;
                    protectedBreakTap.Padding = new Padding(Settings.Instance.ProtectedBorderPanding); // 设置边框粗细
                    protectedBreakTap.ForeColor = Settings.Instance.ProtectedBroderColor; // 设置边框颜色

                    // 根据 x 值设置水平位置
                    int maxTrackWidth = (int)(Settings.Instance.WinWidth * 0.9) / Settings.Instance.MaxKeysQuantity;
                    protectedBreakTap.Left = (int)(Settings.Instance.WinWidth * 0.05 + (maxTrackWidth - protectedBreakTap.Width) / 2 + maxTrackWidth * (element.X - 1));



                    // 初始垂直位置在窗口顶部
                    protectedBreakTap.Top = 0;

                    game.Controls.Add(protectedBreakTap);

                    currentProtectedBreakTaps.Add(protectedBreakTap);
                    protectedBreakTap.BringToFront();
                }
                else
                {
                    continue;
                }
            }
        }

        internal static void TapsMove(Game game, List<ProtectedBreakTap> currentProtectedBreakTaps, int currentTime)
        {
            foreach (var protectedBreakTap in currentProtectedBreakTaps.ToList())
            {
                // 移除准备清除的音符
                if (game.protectedBreakTapsToRemove.Contains(protectedBreakTap))
                {
                    game.Controls.Remove(protectedBreakTap);
                    //game.SetLabelComboText("Remove Successful");
                    game.protectedBreakTapsToRemove.Remove(protectedBreakTap);
                    currentProtectedBreakTaps.Remove(protectedBreakTap);
                    protectedBreakTap.Dispose();
                    continue;
                }

                if (currentTime >= protectedBreakTap.AppearTime)
                {
                    //MessageBox.Show(protectedBreakTap.AppearTime.ToString());
                    int elapsedTime = currentTime - protectedBreakTap.AppearTime;
                    int distance = Settings.Instance.DropSpeed * elapsedTime / Settings.Instance.UnitConversion;
                    //MessageBox.Show(distance.ToString());
                    protectedBreakTap.Top = distance;
                }

                if (protectedBreakTap.Top > Settings.Instance.WinHeight) // 当音符超过窗口高度
                {
                    // 判定为 Miss
                    //game.SetLabelDecideText("MISS");
                    Scoring.ApplyJudgement(game, "MISS", true);

                    // 将音符添加到待移除列表
                    game.protectedBreakTapsToRemove.Add(protectedBreakTap);
                }

            }
        }

        internal static void Judgement(Game game, List<ProtectedBreakTap> currentProtectedBreakTaps, int currentTime, List<Tuple<Keys, int>> keysPress)
        {
            foreach (ProtectedBreakTap protectedBreakTap in currentProtectedBreakTaps.ToList())
            {
                // 跳过已在待移除列表中的音符
                if (game.protectedBreakTapsToRemove.Contains(protectedBreakTap))
                {
                    continue;
                }

                if (Math.Abs(currentTime - protectedBreakTap.JudgeTime) <= 110) // 只在判定时间前后110ms内进行判定
                {
                    foreach (Tuple<Keys, int> key in keysPress.ToList())
                    {
                        if (key.Item1 == Settings.Instance.KeysLocate[protectedBreakTap.Track])
                        {
                            if (Math.Abs(key.Item2 - protectedBreakTap.JudgeTime) <= (int)Settings.JudgeStandard.Good)
                            {
                                // Critical Perfect, Perfect, Great, Good
                                game.protectedBreakTapsToRemove.Add(protectedBreakTap);
                                //game.SetLabelDecideText("CRITICAL PERFECT");
                                Scoring.ApplyJudgement(game, "CRITICAL PERFECT", true);
                            }
                            else
                            {
                                // Miss
                                //game.SetLabelDecideText("MISS");
                                Scoring.ApplyJudgement(game, "MISS", false);
                            }
                            // 处理完按键后，从 keysPress 中移除对应的按键记录
                            game.keysPress.Remove(key);
                            break; // 处理完匹配的按键后跳出此层循环
                        }
                    }
                }
            }
        }

        internal new static void NextTaps(Game game, BeatMap map, List<NoteTap> currentTaps, Element_info[] blockElements)
        {
            throw new NotSupportedException("不支持的操作：子类不可调用父类的 NextTaps 方法");
        }

        internal new static void TapsMove(Game game, List<NoteTap> currentTaps, int currentTime)
        {
            throw new NotSupportedException("不支持的操作：子类不可调用父类的 TapsMove 方法");
        }

        internal new static void Judgement(Game game, List<NoteTap> currentTaps, int currentTime, List<Tuple<Keys, int>> keysPress)
        {
            throw new NotSupportedException("不支持的操作：子类不可调用父类的 Judgement 方法");
        }
    }
}
